/**
 * 
 */
package com.bstu.siit.lab1;
import java.util.Random;
/**
 * @author Victoria Sadko
 *
 */
public class Population {
	private int count; // The number of chromosomes in the population
	private int size; // The size of the chromosomes
	private boolean chromosoms [][]; // Chromosomes array (population)
	public boolean[][] getChromosoms() {
		return chromosoms;
	}
	public void setChromosoms(boolean[][] chromosoms) {
		this.chromosoms = chromosoms;
	}
	public int getCount() {
		return count;
	}
	public int getSize() {
		return size;
	}
	/* 
	 * count is number of chromosomes in the new population
	 * size is size of the chromosomes in the new populations
	 */
	public Population (int count, int size)
	{
		Random random = new Random();
		this.count=count;
		this.size=size;
		this.chromosoms=new boolean[this.count][this.size];
		//Random filling
		for (int i=0; i<count; i++)
			for (int j=0; j<size; j++)
					this.chromosoms[i][j]=random.nextBoolean();		
	}
	/*
	 * Print this population
	 */
	public void print()
	{
		for(int i=0;i<count;i++)
		{
			for(int j=0;j<size;j++)
			{
				if (this.chromosoms[i][j]==true)
					System.out.print("1 ");
				else
					System.out.print("0 ");
			}
			System.out.println();
		}
	}
	public void sort()
	{
		int goodPow[]=new int[count]; //goodPow[i] = number of "good" genes in the chromosome[i]
		for (int i=0; i<count; i++)
		{
			goodPow[i]=0;
			for(int j=0; j<size;j++)
				if (this.chromosoms[i][j]==true)
					goodPow[i]++;
		}
		boolean bufArray[]=new boolean[size];
		int bufVar=0;
		for (int i=0;i<count;i++)		//bubble sort
			for(int j=1;j<count;j++)
				if(goodPow[j]>goodPow[j-1])
				{
					bufVar=goodPow[j-1];
					goodPow[j-1]=goodPow[j];
					goodPow[j]=bufVar;
					
					bufArray=this.chromosoms[j-1];
					this.chromosoms[j-1]=this.chromosoms[j];
					this.chromosoms[j]=bufArray;
				}
	}
	/*
	 * The average number of good genes in the chromosomes of the population
	 */
	public int average()
	{
		int goodPow=0;
		for (int i=0; i<count; i++)
		{
			for(int j=0; j<size;j++)
				if (this.chromosoms[i][j]==true)
					goodPow++;	
		}
		goodPow=(int)(goodPow/this.count); //Rounding to an integer
		return goodPow;
	}
	/*
	 * The new generation in the population
	 */
	public void newGeneration()
	{
		int parentsConst=2;	 //The number of parents with one child
		Random random = new Random();
		int parentsBroad=(int)(this.count/parentsConst);	//The number of chromosomes giving children
		boolean children[][]=new boolean[this.count][this.size];	//The population of children
		boolean parents[][]=new boolean[parentsConst][this.size];	//The parents (with one child)
		for(int i=0;i<count;i++)
		{
			for(int k=0;k<parentsConst;k++)			//The choice of parents
				parents[k]=this.chromosoms[random.nextInt(parentsBroad)];
			for(int j=0;j<size;j++)
			{
					int parentsNumber=random.nextInt(parentsConst); //A parent sending a child sign
					children[i][j]=parents[parentsNumber][j];
			}
		}
		this.setChromosoms(children);	//The old population is dead. Children are now new population.
	}
	/*
	 * 
	 */
	public boolean stopMarker()
	{
		int goodPow[]=new int[count];
		for (int i=0; i<count; i++)
		{
			goodPow[i]=0;
			for(int j=0; j<size;j++)
				if (this.chromosoms[i][j]==true)
					goodPow[i]++;
			if (goodPow[i]==size)
				return true;
		}
		return false;
	}
}